package com.lzy.cloud.sms.controller;

import com.lzy.cloud.sms.dtos.MsgCodeDto;
import com.lzy.cloud.sms.facade.SmsFacade;
import com.lzy.cloud.sms.response.BaseSmsReponse;
import com.lzy.cloud.sms.response.SmsConfigReponse;
import com.lzy.cloud.sms.request.SendSmsRequest;
import com.lzy.cloud.sms.service.MsgCodeService;
import com.lzy.cloud.sms.service.SmsConfigService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping(value = "/sms")
@Slf4j
public class SmsController {

    @Autowired
    private SmsFacade smsFacade;

    @Autowired
    private SmsConfigService smsConfigService;

    @Autowired
    private MsgCodeService msgCodeService;


    /**
     * 短信发送
     *
     * @param sendSmsRequest
     * @param param
     * @return
     */
    @RequestMapping(value = "/send")
    public Result send(@RequestBody SendSmsRequest sendSmsRequest, @RequestParam(required = false) String param) {
        Ensure.that(sendSmsRequest).isNotNull("00000010");
        BaseSmsReponse baseSmsReponse = smsFacade.smsSend(sendSmsRequest, (Map<String, Object>) JSONObject.toBean(JSONObject.fromObject(param), Map.class));
        Result result = new Result();
        result.setCode(baseSmsReponse.getError_code() + "");
        result.setDesc(baseSmsReponse.getReason());
        return result;
    }

    /**
     * 取得全部可用短信发送平台
     *
     * @return
     */
    @RequestMapping(value = "/vailable", method = RequestMethod.GET)
    public Result allVailable() {
        return new Result(DozerUtils.maps(smsConfigService.allVailable(), SmsConfigReponse.class));
    }

    /**
     * 检验验证码
     *
     * @param type
     * @param code
     * @param phone
     * @return
     */
    @RequestMapping(value = "/{phone}/{type}/{code}")
    public Result verificationCode(@PathVariable("phone") String phone, @PathVariable("type") Integer type, @PathVariable("code") String code) {
        MsgCodeDto msgCodeDto = msgCodeService.findByTargetAndType(phone, type);
        Ensure.that(msgCodeDto).isNotNull("00000008");
        Ensure.that(msgCodeDto.getStatus().equals(0)).isTrue("00000011");
        Date expiredTime = strToDate(msgCodeDto.getExpiredTime());
        Ensure.that(expiredTime).isNotNull("00000009");
        Ensure.that(expiredTime.after(new Date())).isTrue("00000011");
        Ensure.that(msgCodeDto.getAuthCode().equals(code)).isTrue("00000009");
        msgCodeDto.setUseTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        msgCodeDto.setStatus(1);
        Ensure.that(msgCodeService.update(msgCodeDto) > 0).isTrue("00000012");
        return new Result();
    }

    /**
     * 时间格式转换
     *
     * @param target
     * @return
     */
    private Date strToDate(String target) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(target);
        } catch (Exception e) {
            log.error("时间格式转换失败  target--" + target);
            return null;
        }
    }


}
